package com.jsoft.pds.view.beans.navigation;

import java.net.URL;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

import com.jsoft.pds.view.beans.authentication.UserSessionBean;
import com.jsoft.pds.view.beans.base.BaseBean;
import com.jsoft.pds.view.util.BundleResource;
import com.jsoft.pds.view.util.Menu;
import com.jsoft.pds.view.util.Menu.MenuItem;
import com.jsoft.pds.view.util.Menu.SubMenu;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@ManagedBean(name = "navigationBean")
@SessionScoped
public class NavigationBean extends BaseBean{
	
	private static final long serialVersionUID = -7781531756990407558L;
	
	private MenuModel model;
	private Menu menu;
	private String home = "/WEB-INF/page/pds/home.xhtml";
	private String current = "/WEB-INF/page/pds/home.xhtml";
	
	public NavigationBean(){
		super();
		bundleResource = new BundleResource();
	}
	
	public void buildPanelMenu(UserSessionBean userSessionBean){
		try{
			XStream xstream = new XStream(new DomDriver());
			xstream.processAnnotations(Menu.class);
			xstream.processAnnotations(SubMenu.class);
			xstream.processAnnotations(MenuItem.class);
			menu = new Menu();
			URL url = getClass().getClassLoader().getResource(Menu.URL_MENU);
			menu = (Menu) xstream.fromXML(url);
			
			if(menu.isEmpty()) return;
			
			if(null == model){
				model = new DefaultMenuModel();
			}else{
				model.addElement(null);
			}
			this.buildItemsFromUserProfile(userSessionBean);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void buildItemsFromUserProfile(UserSessionBean userSessionBean){
		String profile = userSessionBean.getProfile();
		
		DefaultSubMenu subMenu = null;
		DefaultMenuItem item = null;
		String command = null;
		String itemLabel = null;
		
		for(SubMenu sub: menu.getSubMenu()){
			
			if(!this.isValidateByProfile(sub.getProfile(), profile)) continue;
			
			if(sub.isMenuItemEmpty()) continue;
			
			itemLabel = getBundleResource().getSrcText(sub.getLabel());
			subMenu = new DefaultSubMenu();
			subMenu.setId(sub.getName());
			subMenu.setLabel(itemLabel);
			if(null != sub.getImage()) subMenu.setIcon(sub.getImage());
			
			for(MenuItem menu : sub.getMenuItem()){
				
				if(!this.isValidateByProfile(menu.getProfile(), profile)) continue;
				
				itemLabel = getBundleResource().getSrcText(menu.getLabel());
				command = "#{navigationBean.menuAction('" + menu.getPage() +"')}";
				item = new DefaultMenuItem();
				item.setId(menu.getName());
				item.setValue(itemLabel);
				if(null != menu.getImage()) item.setIcon(menu.getImage());
				item.setCommand(command);
				item.setAjax(false);
				subMenu.addElement(item);
			}
			model.addElement(subMenu);
		}
	}
	
	private boolean isValidateByProfile(String profile, String userProfile){
		if(null == profile) return true;
		return (!profile.equals(Menu.ALL_PRIVILEGES) ? (profile.contains(userProfile)) : true);
	}
	
	public void goHome(){
		this.menuAction(home);
	}
	
	public void menuAction(String current){
		setCurrent(current);
		System.out.println("NAVIGATION: " + current);
	}
	
	public MenuModel getModel() {
		return model;
	}
	public void setModel(MenuModel model) {
		this.model = model;
	}
	public void setCurrent(String current) {
		this.current = current;
	}
	public String getCurrent() {
		return current;
	}
}
